import os

from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt
from docx.shared import Inches
from docx.styles.styles import Styles

root_path = r'C:\Users\yilun\whut\demo\AutoShip'


def getAllFiles(targetDir):
    files = []
    excludes = ['venv', 'tianshou_test', 'geoms', 'util',
                'cnn_dqn_run.py', 'main.py', 'make_env.py', 'run1.py', 'tainshou_run.py',
                'single_ship_env_s1.py', 'single_ship_continuous_env.py', 'observation.py',
                'line_style.py', 'line_width.py']
    listFiles = os.listdir(targetDir)
    for i in range(0, len(listFiles)):
        if listFiles[i] in excludes or 'test' in listFiles[i]:
            continue
        path = os.path.join(targetDir, listFiles[i])
        if os.path.isdir(path):
            files.extend(getAllFiles(path))
        elif os.path.isfile(path):
            if '.py' in path and '__.py' not in path and '.pyc' not in path:
                # print(path, path.replace(r'C:\Users\yilun\whut\demo\', '/')
                files.append(path.replace('\\', '/'))
    return files


template_path = r'template.docx'
dest = r'C:\Users\yilun\Desktop\船舶智能避碰系统\船舶智能避碰系统-代码.docx'

document = Document(template_path)

# normal_style = document.styles['Normal']
# normal_style.paragraph_format.space_after = Pt(0)
# normal_style.paragraph_format.line_spacing = 1

# style_file_name = document.styles.add_style(name='filename', style_type=WD_STYLE_TYPE.PARAGRAPH)
# style_file_name.base_style = document.styles['Normal']
# font = style_file_name.font
# font.bold = True
# font.name = 'Calibri'
# font.size = Pt(11)
# style_file_content = document.styles.add_style(name='filecontent', style_type=WD_STYLE_TYPE.PARAGRAPH)

files = getAllFiles(root_path)
root_path = root_path.replace('\\', '/')
# for file in files:
for i, file in enumerate(files):
    with open(file, 'r', encoding='utf-8') as f:
        paragraph = document.add_paragraph()
        paragraph.add_run(file.replace(root_path, '')).bold = True
        document.add_paragraph('')
        document.add_paragraph(f.read())
        if i != len(files) - 1:
            document.add_page_break()

document.save(dest)
